package BuilderType.src;


/***
 * @Description
 * 案例3中的builder模式本质上是一种简化的构建模式
 * 经典的Builder 模式与其有一定的不同，如果没有兴趣的同学就可以不用往下读了。
 *
 * Product: 最终要生成的对象，例如 Computer实例。
 *
 * Builder： 构建者的抽象基类（有时会使用接口代替）。其定义了构建Product的抽象步骤，其实体类需要实现这些步骤。
 *             其会包含一个用来返回最终产品的方法Product getProduct()。
 *
 * ConcreteBuilder: Builder的实现类。
 *
 * Director: 决定如何构建最终产品的算法. 其会包含一个负责组装的方法void Construct(Builder builder)
 *              在这个方法中通过调用builder的方法，就可以设置builder，等设置完成后
 *              就可以通过builder的 getProduct() 方法获得最终的产品。
 *
 * @Date 9:29 2022/8/8
 * @Author sunye
 **/
public class Client4 {
    public static void main(String[] args) {
        ComputerDirector director=new ComputerDirector();//1
        ComputerBuilder builder=new MacComputerBuilder("I7处理器","苹果32g");//2
        director.makeComputer(builder);//3
        Computer4 macComputer=builder.getComputer();//4
        System.out.println("mac computer:"+macComputer.toString());
    }
}

class Computer4 {
    private  String cpu;//必须
    private  String ram;//必须
    private  int usbCount;//可选
    private  String keyboard;//可选
    private  String display;//可选

    @Override
    public String toString() {
        return "Computer3{" +
                "cpu='" + cpu + '\'' +
                ", ram='" + ram + '\'' +
                ", usbCount=" + usbCount +
                ", keyboard='" + keyboard + '\'' +
                ", display='" + display + '\'' +
                '}';
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public void setUsbCount(int usbCount) {
        this.usbCount = usbCount;
    }

    public void setKeyboard(String keyboard) {
        this.keyboard = keyboard;
    }

    public void setDisplay(String display) {
        this.display = display;
    }
}

abstract class ComputerBuilder {
    public abstract void setUsbCount();
    public abstract void setKeyboard();
    public abstract void setDisplay();

    public abstract Computer4 getComputer();
}

class MacComputerBuilder extends ComputerBuilder {
    private Computer4 computer;
    public MacComputerBuilder(String cpu, String ram) {
        computer = new Computer4();
        computer.setCpu(cpu);
        computer.setRam(ram);
    }
    @Override
    public void setUsbCount() {
        computer.setUsbCount(2);
    }
    @Override
    public void setKeyboard() {
        computer.setKeyboard("苹果键盘");
    }
    @Override
    public void setDisplay() {
        computer.setDisplay("苹果显示器");
    }
    @Override
    public Computer4 getComputer() {
        return computer;
    }
}
class ComputerDirector {
    public void makeComputer(ComputerBuilder builder){
        builder.setUsbCount();
        builder.setDisplay();
        builder.setKeyboard();
    }
}